<?php

/**
 * uniBoard API Imageset
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_imageset_image extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Image ID
	 *
	 * @var integer
	 */
	
	public $image_id 		= 0;
	
	/**
	 * Imageset ID
	 *
	 * @var integer
	 */
	
	public $imageset_id 	= 0;
	
	/**
	 * Image Name
	 *
	 * @var string
	 */
	
	public $image_name		= '';
	
	/**
	 * Image File
	 *
	 * @var string
	 */
	
	public $image_file		= '';
	
	/**
	 * Image width
	 *
	 * @var integer
	 */
	
	public $image_width 	= 0;
	
	/**
	 * Image height
	 *
	 * @var integer
	 */
	
	public $image_height 	= 0;
				
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Store image_id
		$this -> image_id = (integer) $target_id;
		
		// Autoload If Image ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'imagesets_images
			WHERE image_id = \'' . $this -> image_id . '\'
			LIMIT 1');
			
			// Style found
			if ( $style_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded		= true;
				
				$this -> imageset_id 		= $style_result['imageset_id'];
				$this -> image_name 		= $style_result['image_name'];
				$this -> image_file 		= $style_result['image_file'];
				$this -> image_width 		= $style_result['image_width'];
				$this -> image_height 		= $style_result['image_height'];
			}
			else
			{
				//give action an error
				$this -> setError(  $appCore -> lang['acp_imageset_image_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> image_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'imagesets_images');
			
				// Store new imageset id
				$this -> image_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'imagesets_images', 'image_id = \'' . $this -> image_id . '\'');
			}
			
			// Flush cache
			class_cache::flushCache( 'imageset_' . $this -> imageset_id);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data from DB and filesystem
	 *
	 */
	
	function deleteData(){
		
		// Delete File?
		if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/' . $this -> image_file))
		{
			// Unlink it from filesystem
			unlink( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/' . $this -> image_file);
		}
		
		// Delete set's cache
		class_cache::flushCache( 'imageset_' . $this -> imageset_id);
		
		// Delete records in database
		$this -> appCore -> db -> delete( 'imagesets_images', 'image_id = \'' . $this -> image_id . '\'');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set imageset_id
	 *
	 * @param integer $new_value
	 */
	
	function setImagesetId( $new_value){
		
		// Store in object
		$this -> imageset_id = (int) $new_value;
		
		// Add to made changes
		$this -> changes['imageset_id'] = $this -> imageset_id;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set image_name
	 *
	 * @param string $new_value
	 */
	
	function setImageName( $new_value){
		
		// Fix name
		$new_value = $new_value;
		
		// Empty image name?
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> image_name = $new_value;
			
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_imageset_image_name_too_long']);	
			}
			else
			{
				// Select image from DB
				$this -> appCore -> db -> query( 'SELECT * FROM ' . DB_PREFIX . 'imagesets_images
				WHERE image_id <> \'' . $this -> image_id . '\' AND imageset_id = \'' . $this -> imageset_id . '\' AND image_name LIKE \'' . $this -> appCore -> db -> strEscape( $new_value, true) . '\'
				ORDER BY image_name');				
				
				// Image exists?
				if ( $this -> appCore -> db -> numRows() > 0)
				{
					// Set Error
					$this -> setError( $this -> appCore -> lang['acp_imageset_image_name_collision']);	
				}
				else
				{
					// Add to made changes
					$this -> changes['image_name'] = $this -> appCore -> db -> strEscape( $this -> image_name);
				}
				
				// Free it
				$this -> appCore -> db -> freeResult();
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_imageset_image_name_empty']);	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set image_file
	 *
	 * @param string $new_value
	 */
	
	function setImageFile( $new_value){
		
		// Empty image name?
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> image_file = $new_value;
				
			// Add to made changes
			$this -> changes['image_file'] = $this -> appCore -> db -> strEscape( $this -> image_file);
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_imageset_image_file_empty']);	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set image_width
	 *
	 * @param integer $new_value
	 */
	
	function setImageWidth( $new_value){
		
		// Store in object
		$this -> image_width = (int) $new_value;
		
		// Fix min value?
		if ( $this -> image_width < 1)
			$this -> image_width = 1;
		
		// Add to made changes
		$this -> changes['image_width'] = $this -> image_width;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set image_height
	 *
	 * @param integer $new_value
	 */
	
	function setImageHeight( $new_value){
		
		// Store in object
		$this -> image_height = (int) $new_value;
		
		// Fix min value?
		if ( $this -> image_height < 1)
			$this -> image_height = 1;
			
		// Add to made changes
		$this -> changes['image_height'] = $this -> image_height;
		
	}
	
}